package rush.edu.wechat.common.service.druid;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import rush.edu.wechat.common.service.db.DbPool;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * DESC:
 * USER:hmily
 * DATE:2017/11/5
 * TIME:17:35
 */
@Configuration
public abstract class DbPoolConfig {

    @Bean
    public DataSource dataSourcePool(DbPool poolConfig) throws SQLException {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        dataSource.setMinIdle(poolConfig.getMinPoolSize());
        dataSource.setMaxActive(poolConfig.getMaxPoolSize());
        dataSource.setMaxWait(poolConfig.getBorrowConnectionTimeout());
        dataSource.setLoginTimeout(poolConfig.getLoginTimeout());
        return dataSource;
    }

}
